package com.atguigu.syt.cmn.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.atguigu.common.util.result.Result;
import com.atguigu.syt.cmn.service.RegionService;
import com.atguigu.syt.model.cmn.Region;
import com.atguigu.syt.vo.cmn.RegionExcelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-05-24
 */
@Api(tags = "地区")
@RestController
@RequestMapping("/admin/cmn/region")
@Slf4j
public class AdminRegionController {
    @Resource
    private RegionService regionService;

    @ApiOperation(value = "根据上级code获取子节点数据列表")
    @ApiImplicitParam(name = "parentCode", value = "上级节点code", required = true)
    @GetMapping(value = "/findRegionListByParentCode/{parentCode}")
    public Result<List<Region>> findRegionListByParentCode(
            @PathVariable String parentCode) {
        List<Region> regionList = regionService.findRegionListByParentCode(parentCode);
        return Result.ok(regionList);
    }

    @ApiOperation(value="导出")
    @GetMapping(value = "/exportData")
    public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {

            //int i = 9/0;
            List<RegionExcelVo> regionExcelVoList = regionService.findRegionExcelVoList();

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("数据字典", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), RegionExcelVo.class)
                    .sheet("数据字典")
                    .doWrite(regionExcelVoList);

        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            // 重置response
            response.reset();
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("导出失败");
        }
    }


    @ApiOperation(value = "导入")
    @ApiImplicitParam(name = "file", value = "文件", required = true)
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        regionService.importData(file);
        return Result.ok();
    }

}

